<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账单编号" prop="billNo">
        <el-input
          v-model="queryParams.billNo"
          placeholder="请输入账单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账单月份" prop="billMonth">
        <el-input
          v-model="queryParams.billMonth"
          placeholder="请输入账单月份"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="老人外键编号" prop="elderlyId">
        <el-input
          v-model="queryParams.elderlyId"
          placeholder="请输入老人外键编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="每月应付" prop="monthlyPayable">
        <el-input
          v-model="queryParams.monthlyPayable"
          placeholder="请输入每月应付"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本期应付" prop="currentPayable">
        <el-input
          v-model="queryParams.currentPayable"
          placeholder="请输入本期应付"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预交款支付金额" prop="prepaymentAmount">
        <el-input
          v-model="queryParams.prepaymentAmount"
          placeholder="请输入预交款支付金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账单金额" prop="billAmount">
        <el-input
          v-model="queryParams.billAmount"
          placeholder="请输入账单金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应付金额" prop="payableAmount">
        <el-input
          v-model="queryParams.payableAmount"
          placeholder="请输入应付金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付截至日期" prop="paymentDeadline">
        <el-date-picker clearable
          v-model="queryParams.paymentDeadline"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择支付截至日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="账单开始时间" prop="billStartTime">
        <el-date-picker clearable
          v-model="queryParams.billStartTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择账单开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="账单结束时间" prop="billEndTime">
        <el-date-picker clearable
          v-model="queryParams.billEndTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择账单结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入创建人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付渠道" prop="paymentChannel">
        <el-input
          v-model="queryParams.paymentChannel"
          placeholder="请输入支付渠道"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentMethod">
        <el-input
          v-model="queryParams.paymentMethod"
          placeholder="请输入支付方式"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联订单" prop="relatedOrder">
        <el-input
          v-model="queryParams.relatedOrder"
          placeholder="请输入关联订单"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="额外字段1" prop="extraField1">
        <el-input
          v-model="queryParams.extraField1"
          placeholder="请输入额外字段1"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="额外字段2" prop="extraField2">
        <el-input
          v-model="queryParams.extraField2"
          placeholder="请输入额外字段2"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['le:bill:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['le:bill:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['le:bill:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['le:bill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="billList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="账单编号" align="center" prop="billNo" />
      <el-table-column label="账单类型" align="center" prop="billType" />
      <el-table-column label="账单月份" align="center" prop="billMonth" />
      <el-table-column label="老人外键编号" align="center" prop="elderlyId" />
      <el-table-column label="每月应付" align="center" prop="monthlyPayable" />
      <el-table-column label="本期应付" align="center" prop="currentPayable" />
      <el-table-column label="预交款支付金额" align="center" prop="prepaymentAmount" />
      <el-table-column label="账单金额" align="center" prop="billAmount" />
      <el-table-column label="应付金额" align="center" prop="payableAmount" />
      <el-table-column label="支付截至日期" align="center" prop="paymentDeadline" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.paymentDeadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易状态" align="center" prop="transactionStatus" />
      <el-table-column label="账单开始时间" align="center" prop="billStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.billStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账单结束时间" align="center" prop="billEndTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.billEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="支付渠道" align="center" prop="paymentChannel" />
      <el-table-column label="支付方式" align="center" prop="paymentMethod" />
      <el-table-column label="支付凭证图片" align="center" prop="paymentVoucher" />
      <el-table-column label="支付备注" align="center" prop="paymentRemark" />
      <el-table-column label="取消原因" align="center" prop="cancelReason" />
      <el-table-column label="关联订单" align="center" prop="relatedOrder" />
      <el-table-column label="额外字段1" align="center" prop="extraField1" />
      <el-table-column label="额外字段2" align="center" prop="extraField2" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['le:bill:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['le:bill:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改账单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="billRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账单编号" prop="billNo">
          <el-input v-model="form.billNo" placeholder="请输入账单编号" />
        </el-form-item>
        <el-form-item label="账单月份" prop="billMonth">
          <el-input v-model="form.billMonth" placeholder="请输入账单月份" />
        </el-form-item>
        <el-form-item label="老人外键编号" prop="elderlyId">
          <el-input v-model="form.elderlyId" placeholder="请输入老人外键编号" />
        </el-form-item>
        <el-form-item label="每月应付" prop="monthlyPayable">
          <el-input v-model="form.monthlyPayable" placeholder="请输入每月应付" />
        </el-form-item>
        <el-form-item label="本期应付" prop="currentPayable">
          <el-input v-model="form.currentPayable" placeholder="请输入本期应付" />
        </el-form-item>
        <el-form-item label="预交款支付金额" prop="prepaymentAmount">
          <el-input v-model="form.prepaymentAmount" placeholder="请输入预交款支付金额" />
        </el-form-item>
        <el-form-item label="账单金额" prop="billAmount">
          <el-input v-model="form.billAmount" placeholder="请输入账单金额" />
        </el-form-item>
        <el-form-item label="应付金额" prop="payableAmount">
          <el-input v-model="form.payableAmount" placeholder="请输入应付金额" />
        </el-form-item>
        <el-form-item label="支付截至日期" prop="paymentDeadline">
          <el-date-picker clearable
            v-model="form.paymentDeadline"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择支付截至日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="账单开始时间" prop="billStartTime">
          <el-date-picker clearable
            v-model="form.billStartTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择账单开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="账单结束时间" prop="billEndTime">
          <el-date-picker clearable
            v-model="form.billEndTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择账单结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="支付渠道" prop="paymentChannel">
          <el-input v-model="form.paymentChannel" placeholder="请输入支付渠道" />
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-input v-model="form.paymentMethod" placeholder="请输入支付方式" />
        </el-form-item>
        <el-form-item label="支付凭证图片" prop="paymentVoucher">
          <el-input v-model="form.paymentVoucher" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="支付备注" prop="paymentRemark">
          <el-input v-model="form.paymentRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="取消原因" prop="cancelReason">
          <el-input v-model="form.cancelReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="关联订单" prop="relatedOrder">
          <el-input v-model="form.relatedOrder" placeholder="请输入关联订单" />
        </el-form-item>
        <el-form-item label="额外字段1" prop="extraField1">
          <el-input v-model="form.extraField1" placeholder="请输入额外字段1" />
        </el-form-item>
        <el-form-item label="额外字段2" prop="extraField2">
          <el-input v-model="form.extraField2" placeholder="请输入额外字段2" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Bill">
import { listBill, getBill, delBill, addBill, updateBill } from "@/api/le/bill"

const { proxy } = getCurrentInstance()

const billList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    billNo: null,
    billType: null,
    billMonth: null,
    elderlyId: null,
    monthlyPayable: null,
    currentPayable: null,
    prepaymentAmount: null,
    billAmount: null,
    payableAmount: null,
    paymentDeadline: null,
    transactionStatus: null,
    billStartTime: null,
    billEndTime: null,
    creator: null,
    paymentChannel: null,
    paymentMethod: null,
    paymentVoucher: null,
    paymentRemark: null,
    cancelReason: null,
    relatedOrder: null,
    extraField1: null,
    extraField2: null
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询账单列表 */
function getList() {
  loading.value = true
  listBill(queryParams.value).then(response => {
    billList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    billNo: null,
    billType: null,
    billMonth: null,
    elderlyId: null,
    monthlyPayable: null,
    currentPayable: null,
    prepaymentAmount: null,
    billAmount: null,
    payableAmount: null,
    paymentDeadline: null,
    transactionStatus: null,
    billStartTime: null,
    billEndTime: null,
    creator: null,
    createTime: null,
    paymentChannel: null,
    paymentMethod: null,
    paymentVoucher: null,
    paymentRemark: null,
    cancelReason: null,
    relatedOrder: null,
    extraField1: null,
    extraField2: null
  }
  proxy.resetForm("billRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加账单"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getBill(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改账单"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["billRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBill(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBill(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除账单编号为"' + _ids + '"的数据项？').then(function() {
    return delBill(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('le/bill/export', {
    ...queryParams.value
  }, `bill_${new Date().getTime()}.xlsx`)
}

getList()
</script>
